<!DOCTYPE HTML>
<html>
<head>
<title>Gosub | AutoHotkey</title>
<meta name="description" content="The Gosub statement jumps to the specified label and continues execution until a Return statement is encountered." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>Gosub</h1>

<p>跳转到指定的标签继续执行, 直到遇到 <a href="Return.htm">Return</a>.</p>

<pre class="Syntax"><span class="func">Gosub</span>, Label</pre>
<h2>参数</h2>
<dl>

  <dt>Label(标签)</dt>
  <dd><p>跳转的目标<a href="../misc/Labels.htm">标签</a>, <a href="../Hotkeys.htm">热键标签</a>或<a href="../Hotstrings.htm#label">热字串标签</a>, 这样会执行 <em>Label</em> 下的命令, 直到遇到 Return 或 Exit. <a href="Return.htm">"Return"</a> 会让脚本返回到 Gosub 后的首个命令恢复执行. <a href="Exit.htm">"Exit"</a> 终止<a href="../misc/Threads.htm">当前线程</a>.</p></dd>

</dl>

<h2>备注</h2>
<p>和几乎其他所有命令的参数一样, <em>Label</em> 可以为<a href="../Variables.htm">变量</a>引用(例如 %MyLabel%), 此时将使用变量中的名称作为跳转的目标. 然而, 性能会稍微降低, 因为每次都必须 "查找" 目标标签名而不是只在脚本首次加载时查找一次.</p>
<p>使用类似 %MyLabel% 的动态标签时, 如果目标标签不存在则会显示错误对话框. 要避免此问题, 请预先调用 <a href="IsLabel.htm">IsLabel()</a>. 例如:</p>
<pre>if IsLabel(VarContainingLabelName)
    Gosub %VarContainingLabelName%</pre>
<p>尽管 Gosub 可用于简单, 普通用途的子程序, 不过对于更复杂的情况请考虑使用<a href="../Functions.htm">函数</a>.</p>
<h2>相关</h2>
<p><a href="Return.htm">Return</a>, <a href="../Functions.htm">函数</a>, <a href="IsLabel.htm">IsLabel()</a>, <a href="Block.htm">区块</a>, <a href="Loop.htm">Loop</a>, <a href="Goto.htm">Goto</a></p>
<h2>示例</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>Gosub, Label1 
MsgBox, The Label1 subroutine has returned (it is finished).
return

Label1:
MsgBox, The Label1 subroutine is now running.
return</pre>
</div>

</body>
</html>